package logic

import (
	"autotestservice/testservices/internal/data"
	"context"
	"gitee.com/jmdvirus/jkgo/jk/jklog"
	"math/rand"
	"strconv"
	"time"

	"autotestservice/testservices/internal/svc"
	"autotestservice/testservices/internal/types"

	"github.com/tal-tech/go-zero/core/logx"
)

type AutotestEditTestOptionAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAutotestEditTestOptionAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) AutotestEditTestOptionAddLogic {
	return AutotestEditTestOptionAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AutotestEditTestOptionAddLogic) AutotestEditTestOptionAdd(req types.TestOptionAdd) (*types.TestOptionAddResponse, error) {
	jklog.L().Debugln("test option add")
	res := types.TestOptionAddResponse{
		Code:    0,
		Message: "success",
	}

	rand.Seed(time.Now().UnixNano())
	iid := rand.Int63n(1000000) + 100000
	req.Item.Id = strconv.FormatInt(time.Now().Unix(), 10) + strconv.FormatInt(iid, 10)
	data, err := data.AutoTestOptionAdd(req)
	if err != nil {
		jklog.L().Errorln("add fail ", err)
		res.Code = 10
		res.Message = "db fail"
		return nil, err
	}

	res.Item = *data

	return &res, nil
}
